package com.dk.strategy;

import com.dk.config.CodePart;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
//自动配置
@Configuration
public class PartTypeFactory {
    //生成一个静态的map
    public static Map<String,CodeStrategyHandler> map = new HashMap<>();
    //注入一个list
    @Autowired
    List<CodeStrategyHandler> codeStrategyHandlerList;

    //项目一启动,自动执行此方法中的逻辑
    @PostConstruct
    private void initHandler(){
        for (CodeStrategyHandler codeStrategyHandler : codeStrategyHandlerList) {
            //通过反射获取到CodeStrategyHandler,在通过反射获取到注解
            Class<? extends CodeStrategyHandler> aClass = codeStrategyHandler.getClass();
            //通过反射获取CodePart中的属性值,partType
            String partType = aClass.getAnnotation(CodePart.class).partType();
            //存入map中
            map.put(partType,codeStrategyHandler);
        }
    }
}
